package cn.hd.controller;

import cn.hd.entity.Address;
import cn.hd.entity.Result;
import cn.hd.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class AddressController {

    @Autowired
    private AddressService addressService;


    @RequestMapping("/addAddress")
    public  Object addAddress(Address address,Long uid){
        int i = addressService.addAddress(address);
        if (i!=0){
            int k = addressService.addUserAddressUser(uid, address.getAid());
            if (k!=0){
                return new Result("200","添加成功！",null);
            }else {
                return new Result("400","添加失败!",null);
            }
        }else {
            return new Result("400","添加失败!",null);
        }
    }

    //删除
    @RequestMapping("/deleteAddress")
    public Object deleteAddress(long aid,long uid){
        int i = addressService.deleteAddress(aid);
        if (i!=0){
            int k = addressService.deleteAddressUser(aid,uid);
            if (k!=0){
                return new Result("200","删除成功！",null);
            }else{
                return new Result("400","删除失败!",null);
            }
        }else {
            return new Result("400","删除失败!",null);
        }
    }

    //修改
    @RequestMapping("/updateAddress")
    public Object updateAddress(Address address){
        int i = addressService.updateAddress(address);
        if (i!=0){
            return new Result("200","修改成功！",null);
        }else {
            return new Result("400","修改失败!",null);
        }
    };

    //根据uid查询地址
    @RequestMapping("/selectuidAddress")
    public Object selectuidAddress(long uid){
        List<Address> list = addressService.selectuidAddress(uid);
        if (list.size()!=0){
            return new Result("200","查询成功！",list);
        }else {
            return new Result("400","查询失败！",null);
        }
    }
    //根据订单bid查询地址
    @RequestMapping("/selectByBid")
    public Object selectByBid(long bid){
        Address address = addressService.selectByBid(bid);
        if (address!=null){
            return new Result("200","查询成功！",address);
        }else {
            return new Result("400","查询失败！",null);
        }
    }
    //根据aid查
    @RequestMapping("/selectByaid")
    public Object selectByaid(long aid) {
        Address address = addressService.selectByAid(aid);
        if (address != null) {
            return new Result("200", "查询成功！", address);
        } else {
            return new Result("400", "查询失败！", null);

        }
    }
}
